// pages/searcher/searchResult/searchResult.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: getApp().globalData.IMG_URL,
    // 自定义导航栏的高度
    statusBarHeight: getApp().globalData.statusBarHeight,
    navBarHeight: getApp().globalData.navBarHeight,
    hiddenResultList: true,
    focusTrue: false,
    keywords: "",
    searchKeywordsResultList: [],
    searchResultList: []
  },
  doBack() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var pageObj = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (param) {
      // console.error("参数：" + JSON.stringify(param))
      pageObj.setData({ keywords: param.keywords })
      // 查询数据 keywords
      pageObj.searchGoodsByKeywords(pageObj.data.keywords)
    });
  },
  searchGoodsByKeywords(keyword: string) {
    console.error("searchGoodsByKeywords：" + keyword)
    var that = this
    // const token = wx.getStorageSync('token')
    const preUrl = getApp().globalData.SERVER_URL
    const goodsPageForHomeUrl = preUrl + "fc/goods/client/anon/getGoodsPageByKeyword"
    wx.request({
      url: goodsPageForHomeUrl,
      method: 'GET',
      data: {
        keyword: keyword,
        pageNo: 1,
        pageSize: 10
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        // 'X-Access-Token': token
      },
      success(res: any) {
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          const length = res.data.result.records.length
          for (let index = 0; index < length; index++) {
            const item = res.data.result.records[index]
            if (item != null) {
              const cover = item.goodsCover;
              // console.error("cover ==> " + cover)
              const firstIndex = cover.indexOf(',');
              const coverReal = cover.substring(0, firstIndex);
              // console.error("coverReal ==> " + coverReal)
              item.goodsCover = coverReal

              if (item.goodsName.length > 16) {
                item.goodsName = item.goodsName.substring(0, 16) + "...";
              }
            }
          }
          that.setData({ searchResultList: res.data.result.records })
          // console.info(JSON.stringify(that.data.goodsList))
        } else {
          console.info(JSON.stringify(res))
        }
      },
    })
  },
  searchKeywordsList(keywords: string) {
    var that = this
    // const token = wx.getStorageSync('token')
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "fc/searchKeyword/client/anon/getKeywordsByKeyword"
    wx.request({
      url: url,
      method: 'GET',
      data: {
        keyword: keywords,
        pageNo: 1,
        pageSize: 10
      },
      // header: {
      //   'content-type': 'application/x-www-form-urlencoded', // 默认值
      //   // 'X-Access-Token': token
      // },
      success(res: any) {
        // console.error("结果: " + JSON.stringify(res))
        // wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          that.setData({ searchKeywordsResultList: res.data.result.records })
          console.error("结果: searchKeywordsResultList = " + JSON.stringify(that.data.searchKeywordsResultList))
        } else {
          console.info(JSON.stringify(res))
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.hideKeyboard({
      complete: res => {
        console.log('hideKeyboard res', res)
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  bindfocusEvevnt() {// 输入框2 获取焦点
    this.setData({ hiddenResultList: false, focusTrue: true })
    // 查询关键字列表
    if (this.data.keywords.length > 0) {
      this.searchKeywordsList(this.data.keywords)
    }
    console.error("事件-输入框2 获取焦点" + this.data.keywords)
  },
  bindblurEvevnt() {// 输入框3 失去焦点
    this.setData({ hiddenResultList: true, focusTrue: false })
    console.error("事件-输入框3 失去焦点")
  },
  bindinputEvevnt(event: any) {// 输入框1 输入触发
    const keywordsValue: string = event.detail.value
    if (keywordsValue == null || keywordsValue.length == 0) {// 输入内容为空
      this.doBack()
    } else {
      this.setData({ keywords: keywordsValue })
      console.error("输入触发: 搜索")
      // 查询关键字列表
      this.searchKeywordsList(keywordsValue)
    }
    console.error("输入触发: " + keywordsValue)
  },
  doSearch(event: any) {// 点击软盘确定按键
    this.setData({ hiddenResultList: true, focusTrue: false })
  },
  clickKeywordsSearch(param: any) {// 点击关键字列表
    let keywords = param.currentTarget.dataset.keywords
    console.error("点击关键字列表: " + keywords)
    this.setData({ hiddenResultList: true, focusTrue: false })
    this.setData({ keywords: keywords })

    // 查询数据 keywords
    this.searchGoodsByKeywords(keywords)
  },
  doClearInputKeyword() {// 点击清除输入
    this.doBack()
  },
  gotoGoodsDetail(param: any): void { // 打开商品详情
    wx.navigateTo({
      url: '/pages/goods/goodsDetail/goodsDetail',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { goodsId: param.currentTarget.dataset.id })
      }
    })
  },
})